package com.boolib.animate.commons.utils;




import com.boolib.animate.commons.exception.UserPasswordNotMatchException;
import com.google.common.hash.Hashing;

import java.util.UUID;

/***
 * 需要guava
 */
public class PasswordUtil {



    private static class EncryptUtil {
        public final static String ser1 = "f2e6d52459e34636aef686198aa40361";
        public final static String ser2="2c78013331c62686eb02de45da8c57c510dd540efa8e921303406cc69fda0daf";

        public static String sha256C(String var1,String salt, int count) {
            String x = salt+var1;
            while (count > 0) {
                count--;
                x = Hashing.sha256().hashBytes(x.getBytes()).toString();
            }
            return x;
        }

        public static String sha256C2(String var1,String salt) {
            return sha256C(var1, salt,2);
        }

        public static String sha256C3(String var1,String salt) {
            var1+=ser1;
            return sha256C(var1,salt, 3);
        }


        public static String sha256C5(String var1,String salt) {
            var1+=ser2;
            return sha256C(var1, salt,5);
        }
    }


    /**
     * 获取加密后的密码
     *
     * @param pass 未加密的密码
     * @param salt 数据库的salt
     * @return 加密后密码 和数据库比对
     */
    public static String getEncryptPass2(String pass, String salt) {
        StringBuffer stringBuffer = new StringBuffer();
        return EncryptUtil.sha256C2(pass,salt);
    }

    /**
     * 加密3次
     * @param pass
     * @param salt
     * @return
     */
    public static String getEncryptPass3(String pass, String salt) {
        StringBuffer stringBuffer = new StringBuffer();
        return EncryptUtil.sha256C3(pass,salt);
    }

    /**
     * 加密5次
     * @param pass
     * @param salt
     * @return
     */
    public static String getEncryptPass5(String pass, String salt) {
        return EncryptUtil.sha256C5(pass,salt);
    }

    /**
     * 获取盐
     * @return
     */
    public static String getSalt() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    /**
     * 生成测试盐和密码
     * @param pass  需要加密密码
     * @param type  加密次数
     */
    public static void outTestByPassType(String pass,int type){
        String salt = getSalt();
        System.out.println("生成数据salt:"+salt);
        switch (type){
            case 2: System.out.println(getEncryptPass2(pass,salt));break;
            case 3: System.out.println(getEncryptPass3(pass,salt));break;
            case 5: System.out.println(getEncryptPass5(pass,salt));break;
            default:System.out.println("默认为加密2次："+getEncryptPass2(pass,salt));break;
        }

    }


    /**
     * 校验user 三次
     * @param dbPass
     * @param formPass
     * @param dbSalt
     */
    public static void checkedUserPassword(String dbPass , String formPass,String dbSalt){

        if(!dbPass.equals(getEncryptPass3(formPass,dbSalt))){
            throw new UserPasswordNotMatchException();
        }
    }

    /**
     * 校验ADmin加密5次
     * @param dbPass
     * @param formPass
     * @param dbSalt
     */
    public static void checkedAdminPassword(String dbPass , String formPass,String dbSalt){
        if(!dbPass.equals(getEncryptPass5(formPass,dbSalt))){
            throw new UserPasswordNotMatchException("账号密码不匹配");
        }
    }



}

